<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

    void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;

        if (app.Request.IsAuthenticated &&
            app.User.Identity is FormsIdentity)
        {
            FormsIdentity identity = (FormsIdentity)app.User.Identity;

            // Find out what role (if any) the user belongs to
            EntidadesTO.UsuarioTO usuarioTO = new EntidadesTO.UsuarioTO();
            usuarioTO.NombreUsuario = identity.Name;

            usuarioTO = Controladoras.UsuarioMantenedor.ObtenerInstancia().ObtenerUsuario(usuarioTO);
            
            string[] strRoles;
            ArrayList arrRoles = new ArrayList();

            arrRoles.Add(usuarioTO.DesPerfil);
            strRoles = (string[])arrRoles.ToArray(typeof(string));

            // Create a GenericPrincipal containing the role name
            // and assign it to the current request
            if (strRoles != null)
                app.Context.User = new System.Security.Principal.GenericPrincipal(identity,
                    strRoles);
        }
    }


       
</script>
